

#include "stdAfx.h"
#include "baseapp.h"

#include <wx/stockitem.h>
#include <wx/dnd.h>

#include "Utils.h"


IMPLEMENT_APP(BombsApp)

#ifdef __WXWINCE__
    STDAPI_(__int64) CeGetRandomSeed();
#endif

//////////////////////////////////////////////////////////////////////////
//fuck this
//int g_mainThreadId = 0;
//HANDLE g_watchdogSemaphore = NULL;
//short g_isInCancelBox = 0;
//////////////////////////////////////////////////////////////////////////

// Called to initialize the program
bool BombsApp::OnInit()
{
#ifdef __WXWINCE__
    srand((unsigned) CeGetRandomSeed());
#else
    srand((unsigned) time(NULL));
#endif

    m_frame = new BombsFrame();

    //m_frame->NewGame(bombsID_EASY, false);

    return true;
}

//////////////////////////////////////////////////////////////////////////

BEGIN_EVENT_TABLE(BombsFrame, wxFrame)
    EVT_MENU(wxID_OPEN,           BombsFrame::OnOpen)
    EVT_MENU(wxID_SAVE,           BombsFrame::OnSave)
    EVT_MENU(wxID_EXIT,          BombsFrame::OnExit)
    //EVT_MENU(wxID_ABOUT,         BombsFrame::OnAbout)
    //EVT_MOUSE_EVENTS         ( BombsFrame::OnMouse )
    //EVT_LEFT_DOWN           ( BombsFrame::OnLeftDown )
END_EVENT_TABLE()
//////////////////////////////////////////////////////////////////////////

BombsFrame::BombsFrame()
: wxFrame(NULL, wxID_ANY, wxT("test")
          , wxDefaultPosition
          , wxSize(300, 300)
          , wxDEFAULT_DIALOG_STYLE|wxMINIMIZE_BOX)
{
//    m_game = game;
//    m_easyCorner = false;
//    m_lastLevel = bombsID_EASY;
//
//    SetIcon(wxICON(bombs));
//
#if wxUSE_STATUSBAR
    CreateStatusBar();
#endif

    // Create a menu bar for the frame
    wxMenuBar *menuBar = new wxMenuBar;
    wxMenu *menuFile = new wxMenu;
    menuFile->AppendSeparator();
    menuFile->Append(wxID_OPEN, wxGetStockLabel(wxID_OPEN), wxT("open pic"));
    menuFile->Append(wxID_SAVE, wxGetStockLabel(wxID_SAVE), wxT("save pics"));
    menuFile->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT), wxT("Quits the application"));

    menuBar->Append(menuFile, wxT("&File"));

    SetMenuBar(menuBar);

    //// Create child subwindows.
    //m_canvas = new BombsCanvas(this, m_game);

    //// Ensure the subwindows get resized o.k.
    ////  OnSize(width, height);

    // Centre frame on the screen.
    Centre(wxBOTH);

    // Show the frame.
    Show();

    wxInitAllImageHandlers(); //wx image
    
    //mlistBox = new wxListBox(parent, wxID_ANY);
    //mlistBox->SetDropTarget(new DnDFile(listBox));
    mfileTarget = new CFileDrogTarget();
    SetDropTarget(mfileTarget);    
}

BombsFrame::~BombsFrame()
{
  //wxDELETE(mfileTarget);
}

//////////////////////////////////////////////////////////////////////////
void BombsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
{
    Close();
}

void BombsFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
{
  wxFileDialog dlg(NULL, wxT("Choose a picture"), wxEmptyString, wxEmptyString, 
    wxT("All files (*.*)|*.*|JPG files (*.jpg)|*.jpg|PNG files (*.png)|*.png|GIF files (*.gif)|*.gif|BMP files (*.bmpSrc)|*.bmpSrc")  )
    ;
  dlg.ShowModal();
  wxString strPathFile = dlg.GetPath();
  wxString strFile = dlg.GetFilename();

  if(strPathFile.Length() ){
    HandlePicture(strPathFile);
  }
}

void BombsFrame::OnSave(wxCommandEvent& WXUNUSED(event))
{

}

//void BombsFrame::OnMouse(wxMouseEvent& event)
//{
//  int i=0;
//  i++;
//}

//void BombsFrame::OnLeftDown(wxMouseEvent& event )
//{
//
//}
//////////////////////////////////////////////////////////////////////////
const int c_IMG_CUT_W_PART = 3;
const int c_IMG_CUT_H_PART = 3;
void HandlePicture(const wxString& strFilePathName)
{
  const wxString strPath = CUtils::ExtractFilePath(strFilePathName.GetData() );
  const wxString strExent = CUtils::ExtractFileExt(strFilePathName.GetData() );
  const wxString strFileName = CUtils::ExtractFileNameWithoutExt(strFilePathName.GetData() );

  wxImage imgSrc(strFilePathName);
  wxBitmap bmpSrc(imgSrc);
  wxMemoryDC dcSrc(bmpSrc);
  const UINT imgW = imgSrc.GetWidth();
  const UINT imgH = imgSrc.GetHeight();
  const UINT imgWEach = imgW / c_IMG_CUT_W_PART;
  const UINT imgHEach = imgH / c_IMG_CUT_H_PART;

  wxBitmap tempBmp(imgWEach, imgHEach);
  wxMemoryDC rDc(tempBmp);
  bool ifdcok = rDc.IsOk();
  if(!ifdcok) return;
  rDc.Clear();
  
  UINT imgCount = 0;
  wxString strSave;
  wxString  strNum;
  //strSave = strPath + wxT("\\save");      
  ////strNum = wxString::Format(wxT("%d"), imgCount);
  ////strSave += strNum;
  //strSave += wxT(".bmp");

  for(UINT y=0; y< c_IMG_CUT_H_PART; y++){
    for(UINT x=0; x< c_IMG_CUT_W_PART; x++){
      //wxImage imgDest(strFilePathName);
      //imgDest.Size(wxSize(imgWEach, imgHEach), 
        //wxPoint(0 + imgWEach*x, 0+ imgHEach*y) );
      rDc.Clear();
      rDc.Blit(wxPoint(0,0), wxSize(imgWEach, imgHEach), &dcSrc, wxPoint(imgWEach*x, imgHEach*y) );
      wxImage saveimg = tempBmp.ConvertToImage();

      strSave = strPath + wxT("\\") + strFileName + wxT("-save-");      
      strNum = wxString::Format(wxT("%d"), imgCount);
      strSave += strNum;
      strSave += wxT(".");
      strSave += strExent;
      saveimg.SaveFile(strSave );

      imgCount++;
    }
  }
 
}

//////////////////////////////////////////////////////////////////////////
bool CFileDrogTarget::OnDropFiles(wxCoord , wxCoord ,
                 const wxArrayString& filenames)
{
  const UINT fileNum = filenames.size();
  wxString fileName;
  for(UINT fileIdx=0; fileIdx<fileNum; fileIdx++){
    fileName = filenames[fileIdx];
    HandlePicture(fileName);
  }  
  return true;
}

//////////////////////////////////////////////////////////////////////////
void test()
{
  std::wstring wstr;
  wxString str(wxT("fdsfds") );
  wxString str1(wstr );

  wxString locDes = wstr.substr(0, 1);
  wxString(wstr.c_str() );
}


